import { EventKeyboard, KeyCode } from "cc";
import { Input } from "cc";
import { EventMouse } from "cc";
import { input } from "cc";
import GameInputMgr from "../input/GameInputMgr";
import { InputType } from "../input/InputConfig";
let CC_KEY_CODE = {
    ENTER: KeyCode.ENTER,
    ESCAPE: KeyCode.ESCAPE,
    PERIOD: KeyCode.PERIOD,
    COMMA: KeyCode.COMMA,
    D: KeyCode.KEY_D,
    S: KeyCode.KEY_S,
    BACKSPACE: KeyCode.BACKSPACE,
    CTRL_LEFT: KeyCode.CTRL_LEFT,
    SHIFT_LEFT: KeyCode.SHIFT_LEFT,
    M: KeyCode.KEY_M,
    N: KeyCode.KEY_N,
    B: KeyCode.KEY_B,
    Up: KeyCode.ARROW_UP,
    Left: KeyCode.ARROW_LEFT,
    Down: KeyCode.ARROW_DOWN,
    Right: KeyCode.ARROW_RIGHT,
    MOUSE_LEFT: EventMouse.BUTTON_LEFT,
    MOUSE_RIGHT: EventMouse.BUTTON_RIGHT,
    MOUSE_MIDDLE: EventMouse.BUTTON_MIDDLE,
    MOUSE_4: EventMouse.BUTTON_4,
    MOUSE_5: EventMouse.BUTTON_5,
    MOUSE_6: EventMouse.BUTTON_6,
    MOUSE_7: EventMouse.BUTTON_7,
    MOUSE_8: EventMouse.BUTTON_8,
}

/**
 * 按键对应的值
 */
// let CC_MOUSE_CODE = {

// }
// let BTN_MAP: number[] = [CC_KEY_CODE.MOUSE_LEFT, 0, CC_KEY_CODE.MOUSE_RIGHT]
export default class CocosInputMgr {
    private static ins: CocosInputMgr
    static getInstance() {
        if (!this.ins) {
            this.ins = new CocosInputMgr();
        }
        return this.ins;
    }
    protected platName: string = InputType.CC;
    start() {
        // GameMouseMgr.getInstance().converter.addKeyCode(this.platName, CC_KEY_CODE)
        GameInputMgr.getInstance().converter.addKeyCode(this.platName, CC_KEY_CODE)
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
        input.on(Input.EventType.MOUSE_DOWN, this.onMouseDown, this);
        input.on(Input.EventType.MOUSE_UP, this.onMouseUp, this);
        input.on(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
    }

    onDestroy() {
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
        input.off(Input.EventType.MOUSE_DOWN, this.onMouseDown, this);
        input.off(Input.EventType.MOUSE_UP, this.onMouseUp, this);
        input.off(Input.EventType.MOUSE_MOVE, this.onMouseMove, this);
    }

    protected onKeyDown(event: EventKeyboard) {
        GameInputMgr.getInstance().changeType(this.platName)
        GameInputMgr.getInstance().onKeyBoardPress(this.platName, event.keyCode, null)
    }

    protected onKeyUp(event: EventKeyboard) {

        GameInputMgr.getInstance().onKeyBoardRelease(this.platName, event.keyCode, null)
        // if (event.keyCode == KeyCode.KEY_G) {
        //     GameInputMgr.getInstance().changeType(InputType.STEAM)
        // } else if (event.keyCode == KeyCode.KEY_H) {
        //     GameInputMgr.getInstance().changeType(InputType.CC)
        // }
    }

    protected onMouseDown(event: EventMouse) {
        GameInputMgr.getInstance().changeType(this.platName)
        GameInputMgr.getInstance().onKeyBoardPress(this.platName, event.getButton(), event.getLocation())
        GameInputMgr.getInstance().onMouseDown(this.platName, event.getButton(), event.getLocation())
    }

    protected onMouseUp(event: EventMouse) {
        GameInputMgr.getInstance().onKeyBoardRelease(this.platName, event.getButton(), event.getLocation())
        GameInputMgr.getInstance().onMouseUp(this.platName, event.getButton(), event.getLocation())
    }

    protected onMouseMove(event: EventMouse) {
        GameInputMgr.getInstance().onMouseMove(this.platName, event.getButton(), event.getLocation())
    }
}

